package org.wk.upms.controller;


import com.google.common.base.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.wk.core.common.view.ResponseVo;
import org.wk.core.web.exception.GeneralException;
import org.wk.upms.enums.ResourceType;
import org.wk.upms.service.ResourceService;
import org.wk.upms.view.MenuVo;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统菜单
 */
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController extends AbstractController {
	@Autowired
	private ResourceService sysMenuService;

	/**
	 * 导航菜单
	 */
	@RequestMapping("/nav")
	public ResponseVo nav(){
		//TODO 暂时为所有菜单，以后做权限控制
		String userId = getUserId();

		List<MenuVo> menuList = sysMenuService.selectMenuByUserId(userId);
		return ResponseVo.success().setData(menuList);
	}
	
	/**
	 * 所有菜单列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:menu:list")
	public List<MenuVo> list(){
		List<MenuVo> menuList = sysMenuService.selectList(null);
		if (menuList==null) {
			return new ArrayList<>();
		}
		return menuList;
	}
	
	/**
	 * 选择菜单(添加、修改菜单)
	 */
	@RequestMapping("/select")
	@RequiresPermissions("sys:menu:select")
	public ResponseVo select(){
		//查询列表数据
		List<MenuVo> menuList = sysMenuService.queryNotButtonList();
		return ResponseVo.success().setData(menuList);
	}
	
	/**
	 * 菜单信息
	 */
	@RequestMapping("/info/{menuId}")
	@RequiresPermissions("sys:menu:info")
	public ResponseVo info(@PathVariable("menuId") String menuId){
		MenuVo menu = sysMenuService.selectById(menuId);
		return ResponseVo.success().setData(menu);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@RequiresPermissions("sys:menu:save")
	public ResponseVo save(@RequestBody MenuVo menu){
		//数据校验
		verifyForm(menu);
		
		sysMenuService.insert(menu);
		
		return ResponseVo.success();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sys:menu:update")
	public ResponseVo update(@RequestBody MenuVo menu){
		//数据校验
		verifyForm(menu);

		sysMenuService.updateById(menu);
		
		return ResponseVo.success();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sys:menu:delete")
	public ResponseVo delete(String menuId){
		//判断是否有子菜单或按钮
		List<MenuVo> menuList = sysMenuService.queryListParentId(menuId);
		if(menuList.size() > 0){
			return ResponseVo.error("请先删除子菜单或按钮");
		}
		sysMenuService.delete(menuId);

		return ResponseVo.success();
	}
	
	/**
	 * 验证参数是否正确
	 */
	private void verifyForm(MenuVo menu){
		//资源名称
		if(Strings.isNullOrEmpty(menu.getName())){
			throw new GeneralException("菜单名称为空");
		}
		//资源父类ID
		if(menu.getParentId() == null){
			throw new GeneralException("上级菜单不能为空");
		}
		//资源分类
		ResourceType resourceType = ResourceType.containsCode(menu.getTypeCode());
		if (resourceType==null) {
			throw new GeneralException("菜单类型不正确");
		}
		//如果是菜单，必须填写 url
		if(resourceType.equals(ResourceType.MENU)){
			if(Strings.isNullOrEmpty(menu.getUrl())){
				throw new GeneralException("菜单URL不能为空");
			}
		}

		//上级菜单类型 默认为目录
		ResourceType parentType = ResourceType.CATALOG;
		//如果上级资源ID 不是O，证明不是根目录，获取上级资源类型
		if(!menu.getParentId().equals("0")){
			MenuVo parentMenu = sysMenuService.selectById(menu.getParentId());
			parentType = ResourceType.containsCode(parentMenu.getTypeCode());
		}

		//目录、菜单的上级资源必须为 目录
		if(resourceType.equals(ResourceType.CATALOG) || resourceType.equals(ResourceType.MENU)){
			if(!parentType.equals(ResourceType.CATALOG)){
				throw new GeneralException("上级菜单只能为目录类型");
			}
			return ;
		}

		//按钮的上级资源必须是菜单
		if(resourceType.equals(ResourceType.BUTTON)){
			if(!parentType.equals(ResourceType.MENU)){
				throw new GeneralException("上级菜单只能为菜单类型");
			}
			return ;
		}

	}
}
